home *** CD-ROM | disk | FTP | other *** search
- /*
- ScalperPatch.c
-
- Performs trap patching using Scalper mechanism.
-
- by Mouse Herrel.
- */
-
- #ifndef __GESTALTEQU__
- #include <GestaltEqu.h>
- #endif
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- #ifdef THINK_C
- #ifndef __pascal__
- #include <Pascal.h>
- #endif
- #endif
-
- #ifdef applec
- #ifndef __STRINGS__
- #include <Strings.h>
- #endif
- #endif
-
- #include "ScalperPatch.h"
- #include "Exceptions.h"
- #include "Globals.h"
-
- ScalperHook ScalperPatch::theirHook = 0;
-
- ScalperPatch::ScalperPatch()
- {
- if (!theirHook)
- InstallScalper();
- }
-
- ScalperPatch::~ScalperPatch()
- {
- // only Scalper patches are safe to replace with the old address.
- if (itsInstalled) {
- Set(itsOld);
- itsInstalled = false;
- }
- }
-
- PatchProcPtr ScalperPatch::Get()
- {
- return *((*theirHook)(itsTrap));
- }
-
- void ScalperPatch::Set(PatchProcPtr proc)
- {
- *((*theirHook)(itsTrap)) = proc;
- }
-
- void ScalperPatch::InstallScalper()
- {
- ScalperHook scalperHook;
- PatchVectorPtr dcmdHook;
-
- // see if scalper was already found.
- if (theirHook)
- return;
-
- // see if scalper is lurking out there.
- RememberGlobals();
- if (Gestalt(gestaltScalperPatchProc, (long*)&scalperHook) != noErr)
- throw (eNoScalperErr);
- theirHook = scalperHook;
- dcmdHook = (*theirHook) (0);
-
- // write our hook in over the currently installed one.
- // we should work out a way to have concurrent use of Scalper
- // by more than one process. I suppose one way, is to save the
- // old one, and restore it when done, or just allow a way to register
- // several processes by name! Good idea in fact.
- *dcmdHook = (PatchProcPtr)&Unpatch;
- }
-
- void ScalperPatch::RemoveScalper()
- {
- if (!theirHook)
- return;
-
- Unpatch();
- *(*theirHook)(0) = 0;
- theirHook = 0;
- }
-
- pascal StringPtr ScalperPatch::Unpatch()
- {
- StringPtr retVal;
-
- OpenGlobals();
- retVal = "\pAll patches have been removed.";
- try {
- Patch::RemoveAll();
- } catch {
- static char message[256];
-
- sprintf(message, "An error of type %hd occurred while unpatching.", theException);
- retVal = c2pstr(message);
- }
-
- CloseGlobals();
- return retVal;
- }
-